package com.woniuxy.java106crm.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.Pattern;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class User implements UserDetails {
    private Integer id;
    @NotBlank(message = "姓名不能为空")
    private String username;
    @JsonIgnore //在对象与JSON互转时忽略该属性
    private String password;
    @Pattern(regexp = "1((34[0-8])|(8\\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\\d{1}))\\d{7}",message = "手机号不正确")
    private String tel;
    private String birthday;
    private String status;
    private String department;
    private String image;

    //用户对应多个角色
    private List<Role> roleList;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        //遍历用户的角色权限信息 封装成SimpleGrantedAuthority对象
        roleList.forEach(role ->{
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getName());
            authorities.add(authority);
            //遍历权限
            role.getPermsList().forEach(perms -> {
                authorities.add(new SimpleGrantedAuthority(perms.getName()));
            });
        });
        return authorities;
    }
    //获取用户名
    @Override
    public String getUsername() {
        return username;
    }
    //账户是否没有过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    //账号是否没有被锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    //账号认证信息是否没有过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    //账号是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }

}
